package com.foryou.system.modules.wx.service.impl;

import com.foryou.system.common.utils.Constant;
import com.foryou.system.modules.wx.EnumUtil;
import com.foryou.system.modules.wx.dao.PlaceDao;
import com.foryou.system.modules.wx.dao.SysMsgDao;
import com.foryou.system.modules.wx.entity.PlaceItem;
import com.foryou.system.modules.wx.enums.AuditStatusEnum;
import com.foryou.system.modules.wx.service.PlaceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by Lee.
 */
@Service
public class PlaceServiceImpl implements PlaceService {

    private final PlaceDao placeDao;

    private final SysMsgDao sysMsgDao;

    public PlaceServiceImpl(PlaceDao placeDao, SysMsgDao sysMsgDao) {
        this.placeDao = placeDao;
        this.sysMsgDao = sysMsgDao;
    }

    @Override
    public List<PlaceItem> queryList(Map<String, Object> map) {
        return placeDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return placeDao.queryTotal(map);
    }

    @Override
    @Transactional
    public int update(Map<String, Object> params) {
        int result = placeDao.update(params);
        if (result > 0) {
            String code = String.valueOf(params.get("auditStatus"));
            AuditStatusEnum statusEnum = EnumUtil.getEnumByCode(code, AuditStatusEnum.class);
            String content = buildMsg(String.valueOf(params.get("name")), statusEnum);
            String title = MessageFormat.format(Constant.MSG_TITLE, statusEnum.getDesc());
            params.put("content", content);
            params.put("title", title);
            sysMsgDao.add(params);
        }
        return result;
    }

    @Override
    public int deleteComment(Long commentId) {
        return placeDao.deleteComment(commentId);
    }

    @Override
    public List<PlaceItem> queryNowList() {
        return placeDao.queryNowList();
    }

    private String buildMsg(String name, AuditStatusEnum statusEnum) {
        StringBuilder sb = new StringBuilder();
        sb.append("您发布的场地名称为:");
        sb.append(name);
        sb.append(",");
        sb.append("审核");
        sb.append(statusEnum.getDesc());
        sb.append("。");
        return sb.toString();
    }
}
